/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.schoolnotice.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.campusinfo.common.config.Global;
import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.web.BaseController;
import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.schoolnotice.entity.SchNotice;
import com.lczyfz.campusinfo.modules.schoolnotice.service.SchNoticeService;

/**
 * 校园通知Controller
 * @author zms
 * @version 2018-03-21
 */
@Controller
@RequestMapping(value = "${adminPath}/schoolnotice/schNotice")
public class SchNoticeController extends BaseController {

	@Autowired
	private SchNoticeService schNoticeService;
	
	@ModelAttribute
	public SchNotice get(@RequestParam(required=false) String id) {
		SchNotice entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = schNoticeService.get(id);
		}
		if (entity == null){
			entity = new SchNotice();
		}
		return entity;
	}
	
	@RequiresPermissions("schoolnotice:schNotice:view")
	@RequestMapping(value = {"list", ""})
	public String list(SchNotice schNotice, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SchNotice> page = schNoticeService.findPage(new Page<SchNotice>(request, response), schNotice);
		model.addAttribute("page", page);
		return "modules/schoolnotice/schNoticeList";
	}

	@RequiresPermissions("schoolnotice:schNotice:view")
	@RequestMapping(value = "form")
	public String form(SchNotice schNotice, Model model) {
		model.addAttribute("schNotice", schNotice);
		return "modules/schoolnotice/schNoticeForm";
	}

	@RequiresPermissions("schoolnotice:schNotice:edit")
	@RequestMapping(value = "save")
	public String save(SchNotice schNotice, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, schNotice)){
			return form(schNotice, model);
		}
		schNoticeService.save(schNotice);
		addMessage(redirectAttributes, "保存校园通知成功");
		return "redirect:"+Global.getAdminPath()+"/schoolnotice/schNotice/?repage";
	}
	
	@RequiresPermissions("schoolnotice:schNotice:edit")
	@RequestMapping(value = "delete")
	public String delete(SchNotice schNotice, RedirectAttributes redirectAttributes) {
		schNoticeService.delete(schNotice);
		addMessage(redirectAttributes, "删除校园通知成功");
		return "redirect:"+Global.getAdminPath()+"/schoolnotice/schNotice/?repage";
	}

}